﻿using System;
using msBsc.AppLogic.DataAlc;
using System.Data;

namespace msBsc.WebUi.Custom
{
    public partial class wfForgetPwsReset : msBsc.Cls.BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string stremkey=this.GetFormParam("KEY");
                if(stremkey==string.Empty)
                {
                    ToErrPage("错误的参数调用,密码重置链接无效！");
                    //this.ExecJScript("window.location.href = '/c_RegMailSend.htm';");
                    return;
                }
                this.ExecJScript("$('#txtUrl').val('" + stremkey + "')"); 
                stremkey=msBsc.AppLogic.Cfg.DesCry.uncMe(stremkey,"rmdes");
                string[] arrten=stremkey.Split(',');
                if(arrten.Length !=3)
                {
                    ToErrPage("错误的参数调用,密码重置链接无效！");
                    //this.ExecJScript("window.location.href = '/c_RegMailSend.htm';");
                    return;
                }
                DateTime dtreg=DateTime.Now.AddDays(-2);
                if(!DateTime.TryParse(arrten[2],out  dtreg) || dtreg<DateTime.Now.AddDays(-1))
                {
                    ToErrPage("链接已过期！"); 
                    //this.ExecJScript("window.location.href = '/c_RegMailSend.htm';");
                    return;
                }
            }
        }
        public override void DoAction()
        {
            if (this.strAction == "REGIST_STEP2")
            {
                if (this.GetParamHtValue("txtUrl") != string.Empty
                    && this.GetParamHtValue("PASS1") != string.Empty
                    && this.GetParamHtValue("PASS2") != string.Empty)
                {
                    string stremkey = this.GetParamHtValue("txtUrl");
                    if (stremkey == string.Empty)
                    {
                        this.Alert("错误的参数调用,密码重置链接无效！");
                        //this.ExecJScript("window.location.href = '/c_RegMailSend.htm';");
                        return;
                    }
                    stremkey = msBsc.AppLogic.Cfg.DesCry.uncMe(stremkey, "rmdes");
                    string[] arrten = stremkey.Split(',');
                    if (arrten.Length != 3)
                    {
                        this.Alert("错误的参数调用,密码重置链接无效！");
                        //this.ExecJScript("window.location.href = '/c_RegMailSend.htm';");
                        return;
                    }
                    DateTime dtreg = DateTime.Now.AddDays(-2);
                    if (!DateTime.TryParse(arrten[2], out  dtreg) || dtreg < DateTime.Now.AddDays(-1))
                    {
                        this.Alert("链接已过期！");
                        //this.ExecJScript("window.location.href = '/c_RegMailSend.htm';");
                        return;
                    }

                    if (this.GetParamHtValue("PASS1") != this.GetParamHtValue("PASS2"))
                    {
                        this.Alert("2次输入的密码不一致！");
                        return;
                    }

                    using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
                    {
                        System.Data.DataSet ds = oCDbAccess.QueryBySQL("SELECT TOP 1 E_MAIL FROM SYS_USER WHERE USER_NME='" + arrten[0] + "'");
                        if (ds.Tables[0].Rows.Count <= 0)
                        {
                            this.Alert("错误的参数调用，要重置的帐号不存在,密码重置链接无效！");
                            return;
                        }
                        string strpws = msBsc.AppLogic.Cfg.DesCry.encMe(this.GetParamHtValue("PASS1").Replace("'", ""), "zbhpass");
                        oCDbAccess.ExecuteBySQL("UPDATE SYS_USER SET LOGIN_PSWD='" + strpws + "' WHERE USER_NME='" + arrten[0] + "'");
                    }
                    this.ExecJScript("$('#spncmp').addClass('active');");
                    this.ExecJScript("$('#divInputInfo').hide();");
                    this.ExecJScript("$('#divMailSend').show();");

                }
                else
                {
                    this.Alert("请输入完整的信息！");
                    return;
                }
            }
        }
    }
}